package com.woniuxy02;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-11-11 14:07
 */
public class Test01 {
    public static void main(String[] args)  {
        try (FileInputStream fis = new FileInputStream("a.txt");) {
            byte[] bytes = new byte[3];
            int length;
            while ((length = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static void test07() throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("b.txt");
            byte[] bytes = new byte[3];
            int length;
            while ((length = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

    private static void test06() throws IOException {
        FileInputStream fis = new FileInputStream("b.txt");
        //定义一个byte[]数组，实际项目一般使用1024的整数倍
        //异常读取一个数组
        byte[] bytes = new byte[3];
        //public int read(byte b[])
        //返回值，代表读取了多少个有效字节,-1表示读完了
        //读取的数据在byte[]数组中
        int length = -1;
        while ((length = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, length));
        }
        fis.close();
    }

    private static void test05() throws IOException {
        FileInputStream fis = new FileInputStream("b.txt");
        int mybyte;
        while ((mybyte = fis.read()) != -1) {
            System.out.println((char) mybyte);
        }
        fis.close();
    }

    private static void test04() {
        //try-with-resources,将要调用close关闭资源的类，定义在try(),
        //这段代码执行结束会自动调用close(),必须实现了Closeable接口的类才能这样使用
        try (FileOutputStream fos = new FileOutputStream("b.txt");) {
            fos.write('a');
            fos.write('b');
            fos.write('c');
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test03() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("a.txt");
            fos.write('a');
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void test02() throws IOException {
        FileOutputStream fos = new FileOutputStream("X:\\a.txt");
        //write(byte b[])，一次写一个字节数组
        byte[] bytes = new byte[]{'a', 98, 99, 'A', 66};
        fos.write(bytes);
        //写中文，将中文的字符串，转成byte[]
        fos.write("中文".getBytes());
        fos.write("汉字".getBytes());
        fos.close();
    }

    private static void test01() throws IOException {
        //创建字节输出流对象，数据保存到d:\aaa\a.txt文件中，没有这个文件会自动创建，文件存在覆盖
//        FileOutputStream fos=new FileOutputStream(new File("d:\\aaa\\a.txt"));
//        FileOutputStream fos=new FileOutputStream("d:\\aaa\\a.txt");
        //boolean append,为true,追加
//        FileOutputStream fos=new FileOutputStream("d:\\aaa\\a.txt",true);
        //绝对路径：文件路径从磁盘根目录开始写
        //相对路径：IO相对路径是相对当前项目所在的目录
        FileOutputStream fos = new FileOutputStream("c.txt");
        //字节流写数据，一次写一个字节
        fos.write(97);
        fos.write('b');
        fos.write('c');
        //IO流底层占有大量的系统资源，使用结束以后，调用close()方法清理资源
        fos.close();
    }
}
